package 多线程;

public class Ticket implements Runnable{
    private int num=100;
    public Ticket(){
    }
    public Ticket(int num){
        this.num=num;
    }
    @Override
    public void run(){
        String tn=Thread.currentThread().getName();
        System.out.printf("%s: 线程启动 %n",tn);
        while (true){
            synchronized (this){
                if(num<1)break;
                System.out.printf("%s:售出一张，剩余%d张票%n",tn,--num);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.printf("%s：线程结束%n",tn);
    }
}
